package com.haotunet.app.goldtreasure.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.activity.RealNameAnthActivity;
import com.haotunet.app.goldtreasure.views.MyAlertDialog;

/**
 * 应用程序UI工具包：封装UI相关的一些操作
 *
 * @author AA
 */

public class UIHelper {

    /**
     * 加载数据对话框
     */
    private static Dialog mLoadingDialog;

    /**
     * 显示提示Toast
     *
     * @param context 上下文
     * @param msg     提示内容
     */
    public static Toast showToast(Context context, String msg) {
        Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        toast.show();
        return toast;
    }

    /**
     * 显示加载对话框
     *
     * @param context 上下文
     * @param msg     对话框显示内容
     */
    public static void check(final Activity context, final String msg) {
        if (context != null && !context.isFinishing()) {
            new MyAlertDialog(context).builder().setTitle(context.getString(R.string.system_prompt)).setMsg(msg)
                    .setPositiveButton(context.getString(R.string.action_positive), true,new OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    }).show();
        }
    }

    public static void showCallRemindDialog(final Activity context, final String callNum) {
        if (context != null && !context.isFinishing()) {
            new MyAlertDialog(context).builder().setTitle(context.getString(R.string.reminder))
                    .setMsg(context.getString(R.string.determine_the_number_of_calls) + callNum)
                    .setPositiveButton(context.getString(R.string.action_positive), true,new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + callNum));
                            context.startActivity(intent);
                        }
                    }).setNegativeButton(context.getString(R.string.action_negative), new OnClickListener() {

                @Override
                public void onClick(View v) {

                }
            }).show();
        }
    }

    public static void showRemindDialog(final Activity context, final String msg) {
        if (context != null && !context.isFinishing()) {
            new MyAlertDialog(context).builder().setTitle(context.getString(R.string.reminder)).setMsg(msg)
                    .setPositiveButton(context.getString(R.string.immediate_authentication), true,new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, RealNameAnthActivity.class);
                            context.startActivity(intent);
                        }
                    }).setNegativeButton(context.getString(R.string.temporarily_not_certified), new OnClickListener() {

                @Override
                public void onClick(View v) {

                }
            }).show();
        }
    }

    public static void showBuyRemindDialog(final Activity context, final String msg, final int type) {
        if (context != null && !context.isFinishing()) {
            new MyAlertDialog(context).builder().setTitle(context.getString(R.string.reminder)).setMsg(msg)
                    .setCancelable(false)
                    .setPositiveButton(context.getString(R.string.buy_immediately), false,new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            ActivityUtils.gotoBuyGold(context, 1, "流动金", "0", 1, 0);
                        }
                    }).setNegativeButton(context.getString(R.string.action_negative), new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (type == 1) {
                        context.finish();
                    }
                }
            }).show();
        }
    }

    public static void showDialogForLoading(Context context, String msg, boolean cancelable) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, null);
        TextView loadingText = (TextView) view.findViewById(R.id.id_tv_loading_dialog_text);
        loadingText.setText(msg);

        mLoadingDialog = new Dialog(context, R.style.loading_dialog_style);
        mLoadingDialog.setCancelable(cancelable);
        mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        mLoadingDialog.show();
    }

    /**
     * 关闭加载对话框
     */
    public static void hideDialogForLoading() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.cancel();
        }
    }

}
